package bootstrap

import (
	"github.com/kataras/golog"
	rotatelogs "github.com/lestrrat-go/file-rotatelogs"
	"io"
	"os"
	"sdn-executor/config"
	"sdn-executor/utils/times"
	"time"
)

// InitLogger 初始化logger
func InitLogger(conf *config.LogConfig) *rotatelogs.RotateLogs {
	aggrLog := conf.FilePath + ".%Y%m%d"
	w, err := rotatelogs.New(
		aggrLog,
		rotatelogs.WithLinkName(conf.FilePath),
		rotatelogs.WithMaxAge(time.Duration(conf.MaxAgeHour)*time.Hour),
		rotatelogs.WithRotationTime(24*time.Hour),
		rotatelogs.WithRotationSize(200*1024*1024),
		rotatelogs.WithRotationCount(100),
	)
	if err != nil {
		panic(err)
	}

	multiWriter := io.MultiWriter(os.Stdout, w)
	golog.SetOutput(multiWriter)
	golog.SetTimeFormat(times.TimeFmt)
	return w
}
